package net.monare.mifss.util;

import java.io.IOException;
import java.util.List;
import java.util.Random;

import net.monare.mifss.configurations.pojo.ContentPoolDef;
import net.monare.mifss.configurations.pojo.StorageDef;
import net.monare.mifss.configurations.pojo.StorageMode;

import net.monare.mifss.configurations.pojo.StorageSetDef;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(false)
public class StorageFactory {
	
	@Value( "${test.data.source}" )
	private String testDataSource;
	
	public StorageDef generateTestStorageDefinition(List<ContentPoolDef> pools) throws IOException{
		
		//


        int randomX =randInt(1000,100000);
		
		//
		// create valid test storage definition
		StorageDef storage = new StorageDef();
		storage.setDataSourceName(testDataSource);
		storage.setName("MIFSS_test_"+randomX);
		storage.setMode(StorageMode.WRITE);
		storage.setTitle("Title_" + randomX);
		storage.setTransformationHandlerName(null);
		storage.setChainedToStorageName(null);
		storage.setContentPoolDefs(pools);
		
		return storage;
		
	}

    public StorageSetDef generateTestStorageSet(){

        StorageSetDef storageSetDef = new StorageSetDef();
        storageSetDef.setLocked(false);
        storageSetDef.setName("TEST_StorageSetDef"+randInt(1000,100000));
        storageSetDef.setDescription("desc");

        return  storageSetDef;
    }
	
	public StorageDef generateInvalidStorageDefinition(int levelOfPopulation, List<ContentPoolDef> pools) throws IOException {
		
	
		
		//
		String storageName="BLAH_BLAH_BLAH";

		
		StorageDef storageDefinition = new StorageDef();

		if (levelOfPopulation > 0) {
			storageDefinition.setName(storageName);
		}
		if (levelOfPopulation > 1) {
			storageDefinition.setDataSourceName(testDataSource);
		}
		if (levelOfPopulation > 2) {
			storageDefinition.setContentPoolDefs(pools);
		}
		if (levelOfPopulation > 3) {
			storageDefinition.setMode(StorageMode.WRITE);
		}
		if (levelOfPopulation > 4) {
			storageDefinition.setDescription("Description_" + storageName);
		}
		if (levelOfPopulation > 5) {
			storageDefinition.setTransformationHandlerName("test_transformation_handler");
		}
		if (levelOfPopulation > 6) {
			storageDefinition.setChainedToStorageName(null);

		}
		if (levelOfPopulation > 7) {
			storageDefinition.setTitle("tile_" + storageName);
		}
		
		return storageDefinition;
	}
	
	private static int randInt(int min, int max) {
	    Random rand = new Random();
	    return rand.nextInt((max - min) + 1) + min;
	}
	
}
